//
//  ViewController.m
//  09--Homework2
//
//  Created by 冀永金 on 16/7/26.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "VCOfSecond.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong ) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *proArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *datastring = [NSString stringWithContentsOfFile:@"/Users/jiyongjin/DLS160710/UI/HomeWork/09--Homework2/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrOfPlace = [datastring componentsSeparatedByString:@"\n"];
    //创建一个省数组
    self.proArr = [NSMutableArray array];
    for (NSString *name in arrOfPlace) {
        if (![name hasPrefix:@" "]) {
            //建立省字典
            NSMutableDictionary *dicOfPro = [NSMutableDictionary dictionary];
            [dicOfPro setObject:name forKey:@"proName"];
            //建立市数组
            NSMutableArray *cityArr = [NSMutableArray array];
            [dicOfPro setObject:cityArr forKey:@"cityArr"];
            [_proArr addObject:dicOfPro];
            //NSLog(@"%@",dicOfPro);
        }
        else if ([name hasPrefix:@"  "] && ![name hasPrefix:@"    "]) {
            //建立市字典
            NSMutableDictionary *dicOfCity = [NSMutableDictionary dictionary];
            [dicOfCity setObject:name forKey:@"cityName"];
            //建立区数组
            NSMutableArray *areaArr = [NSMutableArray array];
            [dicOfCity setObject:areaArr forKey:@"areaArr"];
            NSMutableDictionary *cityDic = [_proArr lastObject];
            NSMutableArray *marrCity = cityDic[@"cityArr"];
            [marrCity addObject:dicOfCity];
            //NSLog(@"111%@",dicOfCity);
        }else {
            NSMutableDictionary *proDic = [_proArr lastObject];
            NSMutableArray *cityArr = proDic[@"cityArr"];
            NSMutableDictionary *cityDic = [cityArr lastObject];
            NSMutableArray *areaArr = cityDic[@"areaArr"];
            [areaArr addObject:name];
            //NSLog(@"%@",areaArr);
        }
    }
   // NSLog(@"%@",_proArr);
    [self create];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)create {
    self.tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _proArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     NSArray *arrOfPrinvice = [_proArr[section] objectForKey:@"cityArr"];
    
    return arrOfPrinvice.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"pool"];
        
    }
    NSArray *arrOfPrinvice = [_proArr[indexPath.section] objectForKey:@"cityArr"];
    //NSLog(@"-----%@",arrOfPrinvice);
   NSString *name = [arrOfPrinvice [indexPath.row] objectForKey:@"cityName"];
   // NSLog(@"%@",name);
    NSArray *arr = [name componentsSeparatedByString:@" "];
   // NSLog(@"%@",arr);
    cell.textLabel.text = arr[2];
    cell.detailTextLabel.text = [arr lastObject];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *name = [_proArr[section] objectForKey:@"proName"];
    NSArray *arr = [name componentsSeparatedByString:@" "];
    return [arr firstObject];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VCOfSecond *vc  = [[VCOfSecond alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    NSArray *arrOfPrinvice = [_proArr[indexPath.section] objectForKey:@"cityArr"];
    vc.arr = arrOfPrinvice;
    //NSLog(@"%@",arrOfPrinvice);
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
